package zhoug.common.utils;

import androidx.annotation.ColorInt;

/**
 * 颜色工具
 */
public class ColorUtils {
    private static final String TAG = "ColorUtils";
    public static final int UN_USED_COLOR= 0;//无效颜色
    /**
     *判断颜色是否属于亮色调
     * @param color
     * @return
     */
    public static boolean isLightColor(int color) {
        return androidx.core.graphics.ColorUtils.calculateLuminance(color) >= 0.5;
    }

    /**
     * 获取2个颜色的混合颜色
     * @param color1 颜色1
     * @param color2 颜色2
     * @param ratio 颜色2占比 0-1
     * @return
     */
    public static  int blendARGB(int color1,int color2,float ratio){
        if(ratio<0){
            ratio=0;
        }
        if(ratio>1){
            ratio=1;
        }
       return androidx.core.graphics.ColorUtils.blendARGB(color1,color2,ratio);
    }

    /**
     * 转化为16进制颜色
     * @param color
     * @return
     */
    public static String toHexString(int color){
        return  Integer.toHexString(color);
    }

    /**
     * 给color添加透明度
     *
     * @param alphaPercent     透明度 0f～1f,占原来透明度的百分比
     * @param baseColor 颜色
     * @return
     */
    @Deprecated
    public static int getColorWithAlpha(float alphaPercent, @ColorInt int baseColor) {
        return setColorAlphaPercent(alphaPercent,baseColor);
    }
    /**
     * 颜色转化成16进制字符串 argb
     *
     * @param color
     * @return
     */
    public static String colorToHex(int color) {
        String s = Integer.toHexString(color);
        int zero = 8 - s.length();
        for (int i = 0; i < zero; i++) {
            s = "0" + s;
        }
        return s;
    }

    /**
     * 获取给定的颜色的ARGB值
     *
     * @param color
     * @return in[4] int[0]:alpha,int[1]:red,int[2]:green,int[3]:blue
     */
    public static int[] getARGB(@ColorInt int color) {
        int a = (color >> 24) & 0xff;
        int r = (color >> 16) & 0xff;
        int g = (color >> 8) & 0xff;
        int b = (color) & 0xff;
        return new int[]{a, r, g, b};
    }

    /**
     * 根据给定的args值获取颜色
     *
     * @param argb
     * @return
     */
    public static @ColorInt
    int getColor(int[] argb) {
        if (argb == null || argb.length != 4) {
            throw new IllegalArgumentException("argb length must is 4");
        }
        return argb[0] << 24 | argb[1] << 16 | argb[2] << 8 | argb[3];
    }

    /**
     * 给color添加透明度
     *
     * @param alpha 透明度 0-255
     * @param color 原来的颜色
     * @return
     */
    public static int setColorAlpha(int alpha, @ColorInt int color) {
        int[] argb = getARGB(color);
        if (alpha < 0) {
            alpha = 0;
        } else if (alpha > 255) {
            alpha = 255;
        }
        argb[0] = alpha;
        return getColor(argb);
    }

    /**
     * 给color添加透明度
     *
     * @param alphaPercent 透明度 0-255
     * @param color        原来的颜色
     * @return
     */
    public static int setColorAlphaPercent(float alphaPercent, @ColorInt int color) {
        if (alphaPercent < 0) {
            alphaPercent = 0;
        } else if (alphaPercent > 1) {
            alphaPercent = 1;
        }
        int[] argb = getARGB(color);
        int alpha = (int) (argb[0] * alphaPercent + 0.5);
        if (alpha < 0) {
            alpha = 0;
        } else if (alpha > 255) {
            alpha = 255;
        }
        argb[0] = alpha;
        return getColor(argb);
    }
}
